const logger = require('./logger');

module.exports = {
  getUserInfo(req) {
    let userInfo = {};
    try {
      if (req.session && req.session.passport) {
        userInfo = JSON.parse(req.session.passport.user);
      }

    } catch (err) {
      logger.error(err);
    }

    return userInfo;
  },
  get(req, name, defaultVlaue) {
    let session = req.session.captcha || {};
    if (name) {
      return session[name] || defaultVlaue;
    }
    return session;
  },
  set(req, name, value) {
    let session = (req.session.captcha || {});
    if (typeof name === "object") {
      session = Object.assign(session, name);
    } else {
      session[name] = value;
    }
    req.session.captcha = session;
    req.session.save();
  },
};
